import logging
import time
import os

from fastapi import FastAPI

from config import get_config

def register_log(app: FastAPI):
    '''
    系统运行日志，保存到文件中
    '''
    tody = time.strftime("%Y%m%d")
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)

    formatter = logging.Formatter("%(asctime)s - %(module)s - %(funcName)s - line:%(lineno)d - %(levelname)s - %(message)s")

    settings = get_config()
    path = os.path.dirname(settings.PATH) + "/logs/" + tody + ".log"
    cf = logging.FileHandler(filename=path, mode="a", encoding="utf8")
    cf.setFormatter(formatter)
    logger.addHandler(cf)
